//
//  RootTabBarController.m
//  1229-Weibo.
//
//  Created by 千锋 on 15/12/29.
//  Copyright (c) 2015年 千锋. All rights reserved.
//

#import "RootTabBarController.h"
#import "HomeTableViewController.h"
#import "DiscoveryTableViewController.h"
#import "MessageTableViewController.h"
#import "PersonalTableViewController.h"
#import "WBTabBarButton.h"
#import "WBTabBar.h"

@interface RootTabBarController ()<WBTabBarDelegate>

//自定义tabBar
@property(nonatomic,strong)WBTabBar * wbTabBar;


@end

@implementation RootTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor redColor];
    
    //添加自定义的tabBar
    [self addTabBar];
    
    //添加viewControllers
    [self addViewControllers];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)addTabBar{
    //隐藏系统自己的tabBar
    self.tabBar.hidden = YES;
    
    self.wbTabBar = [[WBTabBar alloc]initWithFrame:CGRectMake(0, SCREEN_SIZE.height - 49, SCREEN_SIZE.width, 49)];
    
    self.wbTabBar.delegate = self;
    //在block 中使用self  ,应该转换为__weak typeof(self) weakSelf = self;
    __weak typeof (self) weakSelf = self;
    self.wbTabBar.block = ^(NSInteger index){
        weakSelf.selectedIndex = index;
    };
    
    [self.view addSubview:self.wbTabBar];
    
    //self.wbTabBar.backgroundColor = [UIColor lightGrayColor];
}


//添加视图控制器
-(void)addViewControllers
{
    //    //1.首页
    //    HomeTableViewController * homeVC = [[HomeTableViewController alloc]init];
    //    
    //    //2.消息
    //    MessageTableViewController * messageVC = [[MessageTableViewController alloc]init];
    //    
    //    //3.发现
    NSArray * vcNameArray = @[@"HomeTableViewController",
                              @"MessageTableViewController",
                              @"DiscoveryTableViewController",
                              @"PersonalTableViewController"];
    //导航控制器名字：
    NSArray * vcTitleArray = @[@"首页",@"消息",@"发现",@"我"];
    //普通状态图片
    NSArray * norImageArray = @[@"tabbar_home",
                                @"tabbar_message_center",
                                @"tabbar_discover",
                                @"tabbar_profile"];
    //高亮状态图片
    NSArray * hlImageArray = @[@"tabbar_home_highlighted",
                               @"tabbar_message_center_highlighted",
                               @"tabbar_discover_highlighted",
                               @"tabbar_profile_highlighted"];
    //选中状态图片
    //    NSArray * selImageArray = @[@"tabbar_home_selected",
    //                                @"tabbar_message_center_selected",
    //                                @"tabbar_discover_selected",
    //                                @"tabbar_profile_selected"];
    for (NSString * vcName in vcNameArray) {
        Class vcClass = NSClassFromString(vcName);
        NSInteger index = [vcNameArray indexOfObject:vcName];
        UITableViewController * vc = [[vcClass alloc]init];
        UINavigationController * navc = [[UINavigationController alloc]initWithRootViewController:vc];
        vc.title = vcTitleArray[index];
        //定义barItem的选中，高亮图片
        [vc.tabBarItem setImage:[[UIImage imageNamed:norImageArray[index]]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ]];
        [vc.tabBarItem setSelectedImage:[[UIImage imageNamed:hlImageArray[index]]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ]];
        //定义barItem 的文字颜色
        NSDictionary * norAttribute = @{NSFontAttributeName:[UIFont systemFontOfSize:10],NSForegroundColorAttributeName:[UIColor lightGrayColor]};
        
        NSDictionary * selAttribute = @{NSFontAttributeName:[UIFont systemFontOfSize:10],NSForegroundColorAttributeName:[UIColor orangeColor]};
        
        [vc.tabBarItem setTitleTextAttributes:norAttribute forState:UIControlStateNormal];
        [vc.tabBarItem setTitleTextAttributes:selAttribute forState:UIControlStateSelected];
        self.wbTabBar.tabBarItem = vc.tabBarItem;
        [self addChildViewController:navc];
        self.wbTabBar.backgroundColor = [UIColor whiteColor];
        //[[NSRunLoop mainRunLoop]addTimer:self.testTimer forMode:NSRunLoopCommonModes];
    }
}

#pragma  mark -- WBTabBarDelegate

//代理的方法,修改tabBarController 当前显示的视图，
-(void)passIndex:(NSInteger)index{
    self.selectedIndex = index;
}

@end
